package com.alibaba.nacos.plugin.datasource.impl.kingbase8;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import com.alibaba.nacos.plugin.datasource.constants.ContextConstant;
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;

import java.util.ArrayList;
import java.util.List;

import static com.alibaba.nacos.plugin.datasource.constants.DefaultConstant.DEFAULT_TENANT_ID;

public class ConfigInfoAggrMapperByKingbase8 extends AbstractKingbase8Mapper implements ConfigInfoAggrMapper {
    @Override
    public MapperResult batchRemoveAggr(MapperContext context) {

        List<String> datumIds = (List)context.getWhereParameter(FieldConstant.DATUM_ID);
        String dataId = (String)context.getWhereParameter(FieldConstant.DATA_ID);
        String group = (String)context.getWhereParameter(FieldConstant.GROUP_ID);
        String tenantId = (String)context.getWhereParameter(FieldConstant.TENANT_ID);

        List<Object> paramList = new ArrayList();
        paramList.add(dataId);
        paramList.add(group);
        paramList.add(tenantId);
        StringBuilder placeholderString = new StringBuilder();

        for(int i = 0; i < datumIds.size(); ++i) {
            if (i != 0) {
                placeholderString.append(", ");
            }

            placeholderString.append('?');
            paramList.add(datumIds.get(i));
        }

        String sql = "DELETE FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND tenant_id = NVL(?,'" + DEFAULT_TENANT_ID + "') AND datum_id IN ("
                + placeholderString + ")";
        return new MapperResult(sql, paramList);
    }

    @Override
    public MapperResult aggrConfigInfoCount(MapperContext context) {
        List<String> datumIds = (List)context.getWhereParameter(FieldConstant.DATUM_ID);
        boolean isIn = (boolean)context.getWhereParameter(FieldConstant.IS_IN);
        String dataId = (String)context.getWhereParameter(FieldConstant.DATA_ID);
        String group = (String)context.getWhereParameter(FieldConstant.GROUP_ID);
        String tenantId = (String)context.getWhereParameter(FieldConstant.TENANT_ID);
        List<Object> paramList = new ArrayList();
        paramList.add(dataId);
        paramList.add(group);
        paramList.add(tenantId);

        StringBuilder sql = new StringBuilder(
                "SELECT count(*) FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND tenant_id = NVL(?,'" + DEFAULT_TENANT_ID + "') AND datum_id");
        if (isIn) {
            sql.append(" IN (");
        } else {
            sql.append(" NOT IN (");
        }
        for (int i = 0; i < datumIds.size(); i++) {
            if (i > 0) {
                sql.append(", ");
            }
            sql.append('?');
            paramList.add(datumIds.get(i));
        }
        sql.append(')');
        return new MapperResult(sql.toString(), paramList);

    }

    @Override
    public MapperResult findConfigInfoAggrIsOrdered(MapperContext context) {
        String dataId = (String)context.getWhereParameter(FieldConstant.DATA_ID);
        String group = (String)context.getWhereParameter(FieldConstant.GROUP_ID);
        String tenantId = (String)context.getWhereParameter(FieldConstant.TENANT_ID);
        List<Object> paramList = new ArrayList();
        paramList.add(dataId);
        paramList.add(group);
        paramList.add(tenantId);
        String sql= "SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND tenant_id = NVL(?,'" + DEFAULT_TENANT_ID + "')";
        return new MapperResult(sql, paramList);
           }

    @Override
    public MapperResult findConfigInfoAggrByPageFetchRows(MapperContext context) {

        String dataId = (String)context.getWhereParameter(FieldConstant.DATA_ID);
        String group = (String)context.getWhereParameter(FieldConstant.GROUP_ID);
        String tenantId = (String)context.getWhereParameter(FieldConstant.TENANT_ID);
        List<Object> paramList = new ArrayList();
        paramList.add(dataId);
        paramList.add(group);
        paramList.add(tenantId);
        String sql = "SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM config_info_aggr WHERE data_id= ? AND "
                + "group_id= ? AND tenant_id = NVL(?,'" + DEFAULT_TENANT_ID + "') ORDER BY datum_id";
        return new MapperResult(sql, paramList);

    }

    @Override
    public MapperResult findAllAggrGroupByDistinct(MapperContext context) {
        String sql="SELECT DISTINCT data_id, group_id, tenant_id FROM config_info_aggr";
        return new MapperResult(sql, CollectionUtils.list(new Object[0]));
    }

    @Override
    public String getTableName() {
        return TableConstant.CONFIG_INFO_AGGR;
    }

    @Override
    public String getFunction(String s) {
        return s;
    }




}
